﻿using MonitorPlatform.Model;
using MonitorPlatform.ViewModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MonitorPlatform.View
{
    /// <summary>
    /// DevicePageView.xaml 的交互逻辑
    /// </summary>
    public partial class DevicePageView : UserControl
    {
        public DevicePageView()
        {
            InitializeComponent();
            this.DataContext = new DevicePageViewModel();
        }
        public string _deviceStatus { get; set; } = "全部";
        public string _deviceCategory { get; set; } = "全部";
        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            RadioButton button = sender as RadioButton;
            string deviceStatus = button.Content.ToString();
            _deviceStatus = deviceStatus;
            ICollectionView view = CollectionViewSource.GetDefaultView(this.icDevice.ItemsSource);

            if (deviceStatus == "全部" && _deviceCategory == "全部")
            {
                view.Filter = null;

                //排序
                //view.SortDescriptions.Add(new SortDescription("DeviceName", ListSortDirection.Descending));
            }
            else if (deviceStatus == "全部")
            {
                view.Filter = new Predicate<object>((o) =>
                {
                    return (o as DeviceModel).DeviceCategory.Equals(_deviceCategory);
                });
            } else if (_deviceCategory == "全部") 
            {
                view.Filter = new Predicate<object>((o) =>
                {
                    return (o as DeviceModel).DeviceStatus.Equals(deviceStatus);
                });
            }
            else
            {
                view.Filter = new Predicate<object>((o) =>
                {
                    return (o as DeviceModel).DeviceStatus.Equals(deviceStatus) && (o as DeviceModel).DeviceCategory.Equals(_deviceCategory);
                });
            }
        }

        private void RadioButton_Click_1(object sender, RoutedEventArgs e)
        {
            RadioButton button = sender as RadioButton;
            string deviceCategory = button.Content.ToString();
            _deviceCategory = deviceCategory;

            ICollectionView view = CollectionViewSource.GetDefaultView(this.icDevice.ItemsSource);

            if (deviceCategory == "全部"&& _deviceStatus == "全部")
            {
                view.Filter = null;

                //排序
                //view.SortDescriptions.Add(new SortDescription("DeviceName", ListSortDirection.Descending));
            }else if (deviceCategory == "全部")
            {
                view.Filter = new Predicate<object>((o) =>
                {
                    return  (o as DeviceModel).DeviceStatus.Equals(_deviceStatus);
                });
            }else if (_deviceStatus == "全部")
            {
                view.Filter = new Predicate<object>((o) =>
                {
                    return (o as DeviceModel).DeviceCategory.Equals(deviceCategory);
                });
            }
            else
            {
                view.Filter = new Predicate<object>((o) =>
                {
                    return (o as DeviceModel).DeviceCategory.Equals(deviceCategory)&& (o as DeviceModel).DeviceStatus.Equals(_deviceStatus);
                });
            }
        }
    }
}
